import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class clientgui extends JFrame implements ActionListener, KeyListener {

	public JTextField username;
	public JTextField conversation;
	public static JTextArea presentArea;
	public static JTextArea userArea;
	
	private static BufferedReader in;
	private static PrintWriter out;
	
	public JLabel Label;
	public JLabel Label2;
	public JLabel Label3;
	public JButton enter;
	public JButton say;
	public JButton leave;
	public static String name;
	static String IP = "localhost";
	static int port = 9800;
	
	public static boolean entermain = false;

	public static void main(String[] args) throws UnknownHostException, IOException {
		
		String line;
		Socket socket = new Socket(IP, port);
		
		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		//out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
		
		clientgui demo = new clientgui();
		demo.setSize(800, 80);
		demo.createGUI();
		demo.setVisible(true);
		
		while(!entermain) {
			
		}
		
		clientgui demo2 = new clientgui();
		demo2.setSize(800, 950);
		demo2.setTitle("User: " + name);
		demo2.createMainGUI();
		//demo2.addWindowListener(this);
		demo2.setVisible(true);
		
		
		while (true) {
			line = in.readLine();
			if(line!= null){
				if(line.equals("ResetScreen")) {
					userArea.setText(null);
				}
				else if(line.equals("RefreshList")) {
					line = in.readLine();
					userArea.append(line + '\n');
				
				}
				else
					presentArea.append(line + '\n');
			}
		}
		
		
		
		
		
	}
	
	public void createGUI() {
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container win = getContentPane();
        win.setLayout(new FlowLayout());
        
        Label = new JLabel("enter your user name");
        win.add(Label);
        
        username = new JTextField(45);
        win.add(username);
        
        enter = new JButton("enter to Chat!");
        win.add(enter);
        enter.addActionListener(this);
	}
	
	private void createMainGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container win = getContentPane();
        win.setLayout(new FlowLayout());
 
        
        Label2 = new JLabel("SAY: ");
        win.add(Label2);
        
        conversation = new JTextField(45);
        win.add(conversation);
        conversation.addKeyListener(this);
        
        say = new JButton("Chat!");
        win.add(say);
        say.addActionListener(this);
        
        leave = new JButton("Leave!");
        win.add(leave);
        leave.addActionListener(this);
        
        presentArea = new JTextArea(40,40);
        win.add(presentArea);
        JScrollPane scrollPane = new JScrollPane(presentArea);
        win.add(scrollPane);
        
        //Label3 = new JLabel("User List");
        //win.add(Label3);
        
        userArea = new JTextArea(40,20);
        win.add(userArea);
        JScrollPane scrollPane2 = new JScrollPane(userArea);
        win.add(scrollPane2);
        
        
        
		
	}


	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == enter)
		{
			name = username.getText();
			dispose();
			//Name = name;
			//System.out.println(Name);
			out.println(name);
			out.flush();
			
			out.println(name + " entered the room!");
			out.flush();
			
			entermain = true;
		}
		else if(e.getSource() == say)
		{
			String text =  conversation.getText();
			conversation.setText("");
			out.println(text);
			out.flush();
		
			

		}
		
		else if(e.getSource() == leave)
		{
			//System.out.println(Name);
			out.println("LEAVE");
			out.flush();
			out.println("/quit");
			out.flush();
			presentArea.append("You can close the window now!" + '\n');
		
			

		}
		
	}
	
	public void keyPressed(KeyEvent evt) {
		
		int iKey = evt.getKeyCode();
		if ((iKey == KeyEvent.VK_ENTER)){
			String text =  conversation.getText();
			conversation.setText("");
			out.println(text);
			out.flush();
		}
	}


	public void keyReleased(KeyEvent e) {
		
	}


	public void keyTyped(KeyEvent e) {
	
	}



}

